package com.aftersale.demo.controller;

import com.aftersale.demo.entity.DbLog;
import com.aftersale.demo.entity.LogType;
import com.aftersale.demo.entity.Result;
import com.aftersale.demo.entity.User;
import com.aftersale.demo.service.MailService;
import com.aftersale.demo.service.UserService;
import com.aftersale.demo.utils.ValidGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.jws.Oneway;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

/**
*@Author  空
*@Date    2023/10/23 20:02
*@Desc
*/
@Api(tags="用户界面")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    MailService mailService;


    @DbLog(operation = "用户",type = LogType.LOGIN)
    @ApiOperation(value="登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody @Validated({ValidGroup.UserName.class,ValidGroup.PassWord.class}) User user){

        return userService.login(user);
    }

    @DbLog(operation = "账号",type = LogType.REGISTER)
    @ApiOperation(value="注册接口")
    @PostMapping("/register")
    public Result register(@RequestBody @Validated({ValidGroup.UserName.class,ValidGroup.PassWord.class,ValidGroup.Email.class, ValidGroup.VerifyCode.class,ValidGroup.RealName.class}) User user){
        return userService.register(user);
    }

    @ApiOperation(value="退出接口")
    @RequestMapping("/logout")
    public Result logout(){
        return userService.logout();
    }

    @GetMapping("/getforgetcode")
    @ApiOperation(value = "获取忘记密码验证码接口")
    public Result getForgetCode(@RequestParam  String userName){
        return mailService.getCode(userName,"忘记密码",false);
    }

    @GetMapping("/getregistercode")
    @ApiOperation(value = "获取注册验证码接口")
    public Result getRegisterCode(@RequestParam  String email){
        return mailService.getCode(email,"注册",true);
    }

    @RequestMapping("/reset")
    @ApiOperation(value = "重置密码接口")
    public Result reSet(@RequestBody @Validated({ValidGroup.UserName.class,ValidGroup.PassWord.class,ValidGroup.VerifyCode.class}) User user){
        return userService.reSet(user);
    }

    @GetMapping("/getinfo")
    @ApiOperation("个人信息接口")
    public Result myInfo(HttpServletRequest request){
        return userService.myInfo(request);

    }

    @PutMapping("/updateinfo")
    @ApiOperation("修改个人信息接口")
    public Result updateInfo(@RequestBody @Validated({ValidGroup.Email.class,ValidGroup.UserName.class,ValidGroup.RealName.class}) User user){
        return  userService.updateInfo(user);
    }

    @PostMapping("/updatepassword")
    @ApiOperation("修改密码接口")
    public Result updatePassword(@RequestBody @Validated({ValidGroup.PassWord.class,ValidGroup.OldPassWord.class}) User user){
        return  userService.updatePassword(user);
    }
}